package com.york.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.york.eduservice.entity.EduSubject;
import com.york.eduservice.excel.SubjectData;
import com.york.eduservice.service.EduSubjectService;
import com.york.utils.exception.BusinessException;
import com.york.utils.exception.result.ResponseEnum;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright(C),2020,嘉盛科技有限公司
 * FileName:SubjectExcelListener
 * Author:yangboxiong
 * Date:2020/11/18 14:22
 * Description:监听器
 * History:
 * <author>       <time>      <version>       <desc>
 * 作者姓名       修改时间        版本号         描述
 *
 * @author 11235
 */
@Slf4j
@NoArgsConstructor //无参
public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {

    List<SubjectData> list = new ArrayList();
    /**
     * 因为SubjectExcelListener不能交给spring进行管理，需要自己new，不能注入其他对象
     * 不能实现数据库操作
     */
    private EduSubjectService subjectService;
    //传入mapper对象
    public SubjectExcelListener(EduSubjectService subjectService) {
        this.subjectService = subjectService;
    }

    /**
     * 读取excel内容,一行一行进行读取
     *
     * @param context
     */
    @Override
    public void invoke(SubjectData data, AnalysisContext context) {
        //文件数据不能为空
        if (data == null) {
            throw new BusinessException(ResponseEnum.FILE_DATA_IS_EMPTY);
        }
        //一行一行读取，每次读取有两个值，第一个值一级分类，第二个值二级分类
        //判断一级分类是否重复
        EduSubject eduSubject = this.existOneSubject(subjectService, data.getOneSubjectName());
        /**
         * 没有相同一级分类，进行添加
         */
        if (eduSubject == null) {
            eduSubject = new EduSubject();
            eduSubject.setParentId("0");
            //设置一级分类名称
            eduSubject.setTitle(data.getOneSubjectName());
            subjectService.save(eduSubject);
        }
        //获取一级分类的id值
        String pid = eduSubject.getId();

        //添加二级分类
        //判断二级分类是否重复
        EduSubject existTwoSubject = this.existTwoSubject(subjectService, data.getTwoSubjectName(), pid);
        if (existTwoSubject == null) {
            eduSubject = new EduSubject();
            eduSubject.setParentId(pid);
            //设置级二分类名称
            eduSubject.setTitle(data.getTwoSubjectName());
            subjectService.save(eduSubject);
        }
        log.info("解析到一条记录: {}", data);
    }

    /**
     * 判断二级分类不能重复添加
     *
     * @param subjectService
     * @param name
     * @return
     */
    private EduSubject existOneSubject(EduSubjectService subjectService, String name) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title", name);
        wrapper.eq("parent_id", "0");
        EduSubject oneSubject = subjectService.getOne(wrapper);
        return oneSubject;
    }

    /**
     * 判断一级分类不能重复添加
     *
     * @param subjectService
     * @param name
     * @param pid
     * @return
     */
    private EduSubject existTwoSubject(EduSubjectService subjectService, String name, String pid) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title", name);
        wrapper.eq("parent_id", pid);
        EduSubject twoSubject = subjectService.getOne(wrapper);
        return twoSubject;
    }

    /**
     * 所有数据解析完成了 都会来调用
     *
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // 这里也要保存数据，确保最后遗留的数据也存储到数据库
        saveData();
        log.info("所有数据解析完成！");
    }

    /**
     * 加上存储数据库
     */
    private void saveData() {
        log.info("{}条数据，开始存储数据库！", list.size());
        log.info("存储数据库成功！");
    }
}
